package com.example.demo.demos.现场调查;

import com.alibaba.fastjson2.JSONArray;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import com.example.demo.demos.web.DetailData;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

/**
 * 付款通知书 明细表格的自定义渲染策略<br/>
 * 1. 填充货品数据 <br/>
 * 2. 填充人工费数据 <br/>
 *
 * @author Sayi
 */
public class DetailTablePolicy1 extends DynamicTableRenderPolicy {

    // 货品填充数据所在行数
    int goodsStartRow = 2;
    // 人工费填充数据所在行数
    int laborsStartRow = 2;

    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        if (null == data) return;
        JSONArray detailData = JSONArray.from(data);

        List<RowRenderData> labors = detailData.toJavaList(RowRenderData.class);

        if (null != labors) {
            table.removeRow(laborsStartRow);
            for (int i = labors.size() - 1; i >= 0; i--) {
                System.out.println("第几行数据 " + (i + 1));

                XWPFTableRow insertNewTableRow = table.insertNewTableRow(laborsStartRow);
                for (int j = 0; j < 5; j++) insertNewTableRow.createCell();
                System.out.println("----------" + labors.get(i));
                // 倒序的 也就是最后一行渲染完毕后开始合并单元格
                if (i == 0) {
                    System.out.println(labors.get(i));
                    TableTools.mergeCellsVertically(table, 0, laborsStartRow, laborsStartRow + 1);

                    TableTools.mergeCellsVertically(table, 0, laborsStartRow + 2, laborsStartRow + 3);

                    TableTools.mergeCellsVertically(table, 0, laborsStartRow + 4, laborsStartRow + 5);

                    TableTools.mergeCellsVertically(table, 1, laborsStartRow + 5, laborsStartRow + 6);
                    TableTools.mergeCellsVertically(table, 2, laborsStartRow + 1, laborsStartRow + 3);
                }
                TableRenderPolicy.Helper.renderRow(table.getRow(laborsStartRow), labors.get(i));
            }
        }
    }

}
